<template>
  <div class="login-container" :style="loginStyle">
    <div class="login-main">
      <el-form ref="ref" :model="model" :rules="rules" class="login-form" label-position="left">
        <div class="title-container">
          <h3 class="title">{{ system_name }}</h3>
        </div>
        <el-form-item prop="username">
          <el-input v-model="model.username" type="text" placeholder="账号/手机/邮箱" prefix-icon="el-icon-user" autocomplete="on" clearable/>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="model.password" type="password" placeholder="请输入密码" prefix-icon="el-icon-lock" autocomplete="on" clearable show-password/>
        </el-form-item>
        <el-form-item v-if="captcha_switch" prop="captcha_code">
          <el-row>
            <el-col :span="20">
              <el-input v-model="model.captcha_code" placeholder="请输入验证码" prefix-icon="el-icon-picture" autocomplete="off" clearable/>
            </el-col>
            <el-col :span="4">
              <el-image :src="captcha_src" fit="fill" alt="验证码" title="点击刷新验证码" style="cursor: pointer;width: 200px;height:36px;float:right" @click="getCaptcha"/>
            </el-col>
          </el-row>
        </el-form-item>
        <div class="remember" style="margin-bottom: 20px">
          <el-checkbox v-model="model.remember" @change="handleRemember">记住密码</el-checkbox>
        </div>
        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import {apiCaptcha, apiSetting} from '@/api/admin/common'

export default {
  name: 'AdminLogin',
  components: {},
  data() {
    return {
      system_name: '',
      loading: false,
      redirect: undefined,
      otherQuery: {},
      captcha_src: '',
      captcha_switch: 0,
      login_logo_url: '',
      login_bg_url: '',
      model: {
        username: '',
        password: '',
        captcha_key: '',
        captcha_code: '',
        remember: false
      },
      rules: {
        username: [{required: true, message: '请输入账号', trigger: 'blur'}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}],
        captcha_code: [{required: true, message: '请输入验证码', trigger: 'blur'}]
      },

      loginStyle: {
        'background-image': ''
      }
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    this.setting()
  },
  mounted() {
  },
  destroyed() {
  },
  methods: {
    // 验证码
    getCaptcha() {
      apiCaptcha({}).then(res => {
        this.captchaSet(res)
      })
    },
    captchaSet(res) {
      this.model.captcha_key = ''
      this.model.captcha_code = ''
      if (res.data.switch) {
        this.captcha_src = res.data.src
        this.model.captcha_key = res.data.key
      }
      this.captcha_switch = res.data.switch
    },
    // 设置
    setting() {
      this.getCaptcha()
      apiSetting().then(res => {
        this.system_name = res.data.system_name
        this.login_logo_url = res.data.login_logo_url
        this.login_bg_url = res.data.login_bg_url
        if (this.login_bg_url) {
          this.loginStyle = {
            'background-image': 'url(' + this.login_bg_url + ')'
          }
        }
        this.$store.dispatch('settings/changeSetting', {key: 'systemName', value: res.data.system_name})
        this.$store.dispatch('settings/changeSetting', {key: 'pageTitle', value: res.data.page_title})
        this.$store.dispatch('settings/changeSetting', {key: 'logoUrl', value: res.data.logo_url})
        this.$store.dispatch('settings/changeSetting', {key: 'faviconUrl', value: res.data.favicon_url})
      })
    },
    // 登录
    handleLogin() {
      this.$refs['ref'].validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('admin/login', this.model).then(() => {
            this.$router.push({
              path: this.redirect || '/',
              query: this.otherQuery
            }).catch(() => {
              this.loading = false
              this.getCaptcha()
            })
          }).catch(() => {
            this.loading = false
            this.getCaptcha()
          })
        } else {
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },

    // 记住密码
    handleRemember(res) {
      this.model.remember = res
    },
  }
}
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    /*color: $cursor;*/
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;

    input {
      /*background: transparent;*/
      border: 0;
      -webkit-appearance: none;
      border-radius: 0;
      /*color: $light_gray;*/
      /*caret-color: $cursor;*/

      &:-webkit-autofill {
        /*box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;*/
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 460px;
    max-width: 100%;
    /*padding: 160px 35px 0;*/
    /*margin: 0 auto;*/
    overflow: hidden;

    z-index: 1;
    background-color: #fff;
    border-radius: 1em;
    border: 1px solid #fff;
    margin: 150px auto;
    padding: 30px 20px 10px 20px;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      /*color: $light_gray;*/
      margin: 0 auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
}
</style>
